import 'package:flutter/material.dart';
import 'package:imooc_flutter_2024/pages/home_page.dart';

import '../pages/expansion_tile_page.dart';
import '../pages/gird_page.dart';
import '../util/navigator_utils.dart';

class TabNavigator extends StatefulWidget {
  const TabNavigator({Key? key}) : super(key: key);

  @override
  State<TabNavigator> createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  final PageController _pageController = PageController(initialPage: 0);
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  int currIndex = 0;
  @override
  Widget build(BuildContext context) {
    NavigatorUtils.setContext(context);

    return Scaffold(
      /// 创建一个pageView 的tab 导航, 禁止左右滑动
      body: PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: _pageController,
        children: const [
          GridPage(),
          ExpansionTilePage(),
          HomePage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: currIndex,
        selectedItemColor: _activeColor,
        unselectedItemColor: _defaultColor,
        items: [
          _bottomNavigationBarItem(Icons.home, '首页', 0),
          _bottomNavigationBarItem(Icons.search, '搜索', 1),
          _bottomNavigationBarItem(Icons.camera_alt, '旅拍', 2),
          _bottomNavigationBarItem(Icons.account_circle, '我的', 3),
        ],
        onTap: (value) {
          _pageController.jumpToPage(value);
          setState(() {
            currIndex = value;
          });
        },
      ),
    );
  }

  BottomNavigationBarItem _bottomNavigationBarItem(
      IconData icon, String title, int index) {
    return BottomNavigationBarItem(
        icon: Icon(
          icon,
          color: _defaultColor,
        ),
        label: title,
        activeIcon: Icon(
          icon,
          color: _activeColor,
        ));
  }
}
